package com.seanzx.resolver.impl;

import com.seanzx.common.Lists;
import com.seanzx.common.Result;
import com.seanzx.constant.SymbolTable;
import com.seanzx.lexical.Token;
import com.seanzx.resolver.IResolver;
import com.seanzx.dp.ProcessorController;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class ShowVariablesResolver implements IResolver<String> {
    ProcessorController processorController = ProcessorController.get();

    @Override
    public boolean matches(List<Token> tokens) {
        boolean isShowVariables = SymbolTable.SHOW.equalsIgnoreCase(tokens.get(0).getValue()) && "VARIABLES".equalsIgnoreCase(tokens.get(1).getValue());
        if (!isShowVariables) {
            return false;
        }
        if (tokens.size() == 4) {
            return "LIKE".equals(tokens.get(2).getValue());
        }
        return tokens.size() == 2;
    }

    @Override
    public String translate(List<Token> tokens) {
        if (tokens.size() == 4) {
            return tokens.get(3).getValue();
        }
        return null;
    }

    @Override
    public boolean validate(Object parameter) {
        return true;
    }

    @Override
    public Result access(Object parameter) {
        if (parameter == null) {
            Result<Set<String>> result = processorController.properties();
            if (result.isSuccess()) {
                List<String> list = result.getData().stream().collect(Collectors.toList());
                return Result.ok(Lists.of(Lists.of("VARIABLES", list)));
            }
            return result;
        }
        // TODO LIKE 实现
        return Result.ok();
    }
}
